﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LX.Core.Utils;
using LX.Web.Mvc;
using S4Store.Service;
using S4Store.Domain;
using S4Store.Models;
using S4Store.Models.Car;

namespace S4Store.Controllers
{
    public class CarModelController : BaseController
    {

        public CarService CarService { get; set; }

        public ActionResult Index()
        {
            CarModelListPage model = new CarModelListPage();
            if (this.CurrentUser.Position == Position.GeneralManager)
            {
                model.AllowAdd = true;
                model.AllowModify = true;
                model.AllowChangeStatus = true;
            }
            if (this.CurrentUser.Position == Position.SystemManager)
            {
                model.AllowAdd = true;
                model.AllowDelete = true;
                model.AllowModify = true;
                model.AllowChangeStatus = true;
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                var brands = CarService.GetCarBrandQuery().ToList();
                model.Brands = brands;
            }
            return View(model);            
        }


        public ActionResult Query()
        {
            QueryModel query = new QueryModel
            {
                Page = 1,
                PageSize = 20
            };
            return Query(query);
        }

        [HttpPost]
        public ActionResult Query(QueryModel query)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);

                var querysource = CarService.GetCarModelQuery();
                if (!Helper.StringIsEmpty(query.Name))
                {
                    querysource = querysource.Where(x => x.Name.Contains(query.Name));
                }
                if (query.BrandId != null)
                {
                    querysource = querysource.Where(x => x.BrandId == query.BrandId);
                }
                if (query.PinPaiId != null)
                {
                    querysource = querysource.Where(x => x.Brand.PinPaiId == query.PinPaiId);
                }
                if (
                           CurrentUser.Position != S4Store.Domain.Position.GeneralManager &&
                           CurrentUser.Position != S4Store.Domain.Position.SystemManager
                           )
                {
                    querysource = querysource.Where(x => x.Brand.PinPaiId == CurrentUser.Store.PinPaiId);
                }
                int total = querysource.Count();
                querysource = querysource.OrderByDescending(x => new { x.BrandId, x.Id });
                if (query.Page > 1 && query.PageSize > 0)
                {
                    querysource = querysource.Skip((query.Page - 1) * query.PageSize);
                }
                querysource = querysource.Take(query.PageSize);
                LX.Core.Query.PagedList<CarModel> data = new LX.Core.Query.PagedList<CarModel>
                {
                    Data = querysource.ToList(),
                    Page = query.Page,
                    PageSize = query.PageSize,
                    Total = total

                };
                return View(data);
            }
        }


        public ActionResult Add()
        {
            using (var t = new Transtion())
            {
                t.Inject<CarService>(CarService);
                if (
                          CurrentUser.Position == S4Store.Domain.Position.GeneralManager ||
                          CurrentUser.Position == S4Store.Domain.Position.SystemManager
                          )
                {
                    var brands = CarService.GetCarBrandQuery().ToList();
                    return View(brands);
                }
                else
                {
                    var brands = CarService.GetCarBrandQuery().Where(x => x.PinPaiId == CurrentUser.Store.PinPaiId).ToList();
                    return View(brands);
                }
            } 

        }

        [HttpPost]
        public JsonResult Add(CarModel model)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                CarService.AddCarModel(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success=true
                    }
                };
            } 
        }

        [HttpPost]
        public ActionResult Modify(int id)
        {
            using (var t = new Transtion())
            {
                t.Inject<CarService>(CarService);
                var carmodel = CarService.GetCarModelQuery().FirstOrDefault(x => x.Id == id);
                var brands = CarService.GetCarBrandQuery().ToList();
                if (
                          CurrentUser.Position != S4Store.Domain.Position.GeneralManager&&
                          CurrentUser.Position != S4Store.Domain.Position.SystemManager
                          )
                {
                    brands = CarService.GetCarBrandQuery().Where(x => x.PinPaiId == CurrentUser.Store.PinPaiId).ToList();
                    return View(brands);
                }
                t.Commit();
                if (carmodel != null)
                {
                    var model = new Combin<List<CarBrand>, CarModel>
                    {
                        Data1 = brands,
                        Data2 = carmodel
                    };
                    return View(model);
                }
                return this.Content("车型不存在");
            }   
            
        }


        [HttpPost]
        public JsonResult ModifyBack(CarModel model)
        {
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                CarService.ModifyCarModel(model);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }

        }

        [HttpPost]
        public JsonResult Delete(List<int> ids)
        {
            CheckPower(Position.SystemManager);
            if(Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要删除的项");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    CarService.DeleteCarModel(id);
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "删除成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Upline(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要上线的品牌");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    var model = CarService.GetCarModelQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = false;
                        CarService.ModifyCarModel(model);
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "操作成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        [HttpPost]
        public JsonResult Downline(List<int> ids)
        {
            if (Helper.ArrayIsEmpty(ids))
            {
                throw new Exception("请选择要下线的品牌");
            }
            using (var t = new Transtion())
            {
                t.Inject(CarService);
                foreach (var id in ids)
                {
                    var model = CarService.GetCarModelQuery().FirstOrDefault(x => x.Id == id);
                    if (model != null)
                    {
                        model.IsClose = true;
                        CarService.ModifyCarModel(model);
                    }
                }
                t.Commit();
            }
            return new JsonResult
            {
                Data = new
                {
                    success = true,
                    result = "操作成功"
                },
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
    }
}
